package cn.edu.djtu.fw.action;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.struts2.interceptor.ServletRequestAware;
import org.springframework.beans.factory.annotation.Autowired;

import cn.edu.djtu.fw.model.Car;
import cn.edu.djtu.fw.model.Json;
import cn.edu.djtu.fw.service.CarServiceI;
import cn.edu.djtu.fw.util.DateUtil;
import cn.edu.djtu.fw.util.JsonUtil;

import com.opensymphony.xwork2.ActionSupport;

public class CarAction extends ActionSupport implements ServletRequestAware {
	private static final long serialVersionUID = 1L;
	private HttpServletRequest request;
	private CarServiceI carService;
	private Car car;
	private int page;
	private int rows;
	private String ids;

	@Override
	public String execute() throws Exception {
		Map<String, Object> m = new HashMap<String, Object>();
		m = carService.getAllCar(page, rows, car);
		JsonUtil.writeJson(m);
		return null;
	}

	public String show() throws Exception {
		HttpSession session = request.getSession();
		Car c = new Car();
		if (locationstart != null && locationstart.length() > 0) {
			c.setLocationstart(locationstart);
		}
		if (starttimeStart != null && starttimeStart.length() > 0) {
			c.setStarttimeStart(DateUtil.stringToDate("1970-01-01 " + starttimeStart + ":00", "yyyy-MM-dd HH:mm:ss"));
		} else {
			c.setStarttimeStart(DateUtil.stringToDate("1970-01-01 00:00:01", "yyyy-MM-dd HH:mm:ss"));
		}
		if (starttimeEnd != null && starttimeEnd.length() > 0) {
			c.setStarttimeEnd(DateUtil.stringToDate("1970-01-01 " + starttimeEnd + ":00", "yyyy-MM-dd HH:mm:ss"));
		} else {
			c.setStarttimeEnd(DateUtil.stringToDate("1970-01-01 23:59:59", "yyyy-MM-dd HH:mm:ss"));
		}
		@SuppressWarnings("unchecked")
		List<Car> s_car = (List<Car>) carService.getAllCar(1, 20, c).get("rows");
		session.setAttribute("s_car", s_car);
		return "car";
	}

	public void add() throws Exception {
		Json j = new Json();
		if (carService.insert(car)) {
			j.setSuccess(true);
			j.setMsg("添加成功！");
		} else {
			j.setMsg("添加失败！");
		}
		JsonUtil.writeJson(j);
	}

	public void update() throws Exception {
		Json j = new Json();
		if (carService.update(car)) {
			j.setSuccess(true);
			j.setMsg("修改成功！");
		} else {
			j.setMsg("修改失败！");
		}
		JsonUtil.writeJson(j);
	}

	public void delete() throws Exception {
		Json j = new Json();
		String[] idsStr = ids.split(",");
		for (int i = 0; i < idsStr.length; i++) {
			Car c = carService.getCarById(Integer.parseInt(idsStr[i]));
			if (c != null) {
				carService.delete(c.getCarid());
			}
		}
		j.setSuccess(true);
		j.setMsg("删除成功！");
		JsonUtil.writeJson(j);
	}

	public int getPage() {
		return page;
	}

	public void setPage(int page) {
		this.page = page;
	}

	public int getRows() {
		return rows;
	}

	public void setRows(int rows) {
		this.rows = rows;
	}

	public String getIds() {
		return ids;
	}

	public void setIds(String ids) {
		this.ids = ids;
	}

	public Car getCar() {
		return car;
	}

	public void setCar(Car car) {
		this.car = car;
	}

	public CarServiceI getCarService() {
		return carService;
	}

	@Autowired
	public void setCarService(CarServiceI carService) {
		this.carService = carService;
	}

	@Override
	public void setServletRequest(HttpServletRequest req) {
		this.request = req;
	}

	private String locationstart;

	private String starttimeStart;
	private String starttimeEnd;

	public String getLocationstart() {
		return locationstart;
	}

	public void setLocationstart(String locationstart) {
		this.locationstart = locationstart;
	}

	public String getStarttimeStart() {
		return starttimeStart;
	}

	public void setStarttimeStart(String starttimeStart) {
		this.starttimeStart = starttimeStart;
	}

	public String getStarttimeEnd() {
		return starttimeEnd;
	}

	public void setStarttimeEnd(String starttimeEnd) {
		this.starttimeEnd = starttimeEnd;
	}

}
